<?php
require_once("../resources/commons.php");
require_once("../data/User.php");
require_once("Mail.php");
require_once("Mail/mime.php");

//WARNING: please configure appropriately the HTPASSWD_FILE_PATH variable in the conf.php file

//------------------------------------------------------------------------------------------
// A basic implementation of the invitation_plugin.php file.
// The method "manageInvitation" is called when the invitation form is submitted.
// This allow the developer to "customize" the invitation process.
//------------------------------------------------------------------------------------------

/**
 * Send an invitation to a user so that he/she could complete his/her registration.<br>
 * This implementation also create an HTTP basic login/password and update an .htpasswd file
 * located at the root of the website.
 * @param string $p_email the user e-mail address
 * @param string $p_profileUpdateKey the long key for the registration script
 */
function manageInvitation($p_email, $p_profileUpdateKey){
	//The registration URL
	$l_profileUpdateUrl=getServerURL(ROOT_PATH . "/apps/register.php?" . PROFILE_UPDATE_KEY . "=" . $p_profileUpdateKey);
	
	// subject
	$l_subject = "IATB registration process";
	
// message
$l_messageHtml=<<<EOT
<html>
<head>
  <title>IATB registration</title>
</head>
<body>
<b>Please do not reply to this e-mail.</b>
<br/>
<p>You have been invited to register to IATB service.</p>
<p>
Plase complete the IATB registration page which is accessible for you through
 this <b><a href="{$l_profileUpdateUrl}">link</a></b>.
</p>
</body>
</html>
EOT;

$l_messageText=<<<EOT
Please do not reply to this e-mail.
You have been invited to register to IATB service.

Plase complete the IATB registration page which is accessible for you through this link: {$l_profileUpdateUrl}
EOT;


$l_crlf = "\n";
$l_headers = array(
	"From"    => "noreply@noreply",
	"Subject" => $l_subject
);

$l_mime = new Mail_mime($l_crlf);
$l_mime->setTXTBody($l_messageText);
$l_mime->setHTMLBody($l_messageHtml);

$l_body = $l_mime->get();
$l_headers = $l_mime->headers($l_headers);

$l_mail=Mail::factory("mail");
$l_mail->send($p_email, $l_headers, $l_body);
 
	?>
	<p>An e-mail has been send to <?=$p_email ?>.</p>
	<p>The user must connect within 48 hours to this <a href="<?=$l_profileUpdateUrl?>">update link</a> to complete the registration process otherwise the account
	will be locked.</p>
	<?php
}

//------------------------------------------------------------------------------------------
// An implementation of the login_plugin.php file.
// The method "manageSession" is called just after the user credentials (login/password)
// have been verified.
// This allow the developer to customize the registration process with some specific actions) 
//------------------------------------------------------------------------------------------

/**
 * Called just after the user login/password have been verified.
 * @param User $p_user the User object extracted from the login
 * @param string $p_backUrl the URL to be called once the authentication is a success
 */
function manageSession($p_user, $p_backUrl){
    //Create a session
    SessionManager::getInstance()->createSession($p_user);
    HTTP::redirect(base64_decode($p_backUrl));
}

?>